package com.drinks.yiniang.ui.activity.shop;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.amap.api.location.AMapLocation;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.shop.ShopListAdapter;
import com.drinks.yiniang.common.recycler.decoration.ItemSpacesDecoration;
import com.drinks.yiniang.common.listener.OnLocationChangesListener;
import com.drinks.yiniang.common.location.MyLocationManager;
import com.drinks.yiniang.entity.shop.ShopJson;
import com.drinks.yiniang.io.api.ShopApiIO;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.hansen.library.ui.widget.refresh.MSwipeRefreshLayout;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;

import java.util.ArrayList;

/**
 * 门店列表
 *
 * @author han
 * @date 2019-11-01
 */
public class ShopListActivity extends BaseTranBarActivity implements OnNavgationBarClickListener,
        SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemClickListener, OnLocationChangesListener {

    //nav
    private NavigationBarLayout nav_bar_shop_list;
    //refresh
    private MSwipeRefreshLayout refresh_shop_list;
    //列表
    private BaseRecyclerView rcv_shop_list;

    private ShopListAdapter mShopListAdapter;

    private MyLocationManager mLocationManager;

    private double mLng, mLat;

    //是否是选择门店操作
    private boolean isChooseShop;

    @Override
    protected void initView() {
        nav_bar_shop_list = findViewById(R.id.nav_bar_shop_list);

        refresh_shop_list = findViewById(R.id.refresh_shop_list);

        rcv_shop_list = findViewById(R.id.rcv_shop_list);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        isChooseShop = getIntent() != null ? getIntent().getBooleanExtra(Constants.KeyChoose, false) : false;

        initAdapter();

        showLoadingDialog(R.string.text_location_dot);

        mLocationManager = MyLocationManager.getInstance(mContext);
        mLocationManager.initLocation();
        mLocationManager.startLocation();
    }

    private void initAdapter() {
        rcv_shop_list.setLayoutManager(new LinearLayoutManager(mContext));
        rcv_shop_list.addItemDecoration(ItemSpacesDecoration.getLRTItemCountDPDecoration(10));

        mShopListAdapter = new ShopListAdapter(mContext, new ArrayList<ShopJson.ShopList>());
        mShopListAdapter.setCenterEmpty(getLayoutInflater(), rcv_shop_list);
        mShopListAdapter.bindToRecyclerView(rcv_shop_list);
    }

    @Override
    protected void initListener() {
        nav_bar_shop_list.setOnNavgationBarClickListener(this);

        refresh_shop_list.setOnRefreshListener(this);

        mShopListAdapter.setOnItemClickListener(this);
        mLocationManager.setLocationChangeListener(this);
    }

    @Override
    public void widgetClick(View v) {

    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_shop_list;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {

    }

    @Override
    public void onRefresh() {
        getShopList();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        ShopJson.ShopList data = mShopListAdapter.getItem(position);

        if(data == null) {
            return;
        }

        if(isChooseShop) {//是选择操作
            Intent intent = new Intent();
            intent.putExtra(Constants.KeyObject, data);
            setResult(Constants.RESULT_CODE_NORMAL, intent);
            finish();
        } else {
            Intent intent = new Intent(mContext, ShopMapActivity.class);
            intent.putExtra(Constants.KeyObject, data);
            startActivity(intent);
        }
    }

    @Override
    public void locationChange(AMapLocation aMapLocation) {
        if (aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
                mLng = aMapLocation.getLongitude();
                mLat = aMapLocation.getLatitude();
            }
        }

        getShopList();
    }

    //获取店铺列表
    private void getShopList() {
        if(!refresh_shop_list.isRefreshing()) {
            showLoadingDialog(R.string.text_waiting_loading);
        }

        ShopApiIO.getInstance().getShopList(mLng + "," + mLat, new APIRequestCallback<ShopJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(ShopJson res) {
                if (mShopListAdapter == null) {
                    return;
                }

                mShopListAdapter.getData().clear();
                mShopListAdapter.addData(res.getData());
                mShopListAdapter.loadMoreEnd();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
                mShopListAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {
                refresh_shop_list.setRefreshing(false);
                dismissDialog();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLocationManager.stopLocation();
        mLocationManager.destroyLocation();
    }
}
